package com.lipy.lock.distributed.zk;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author lipye
 */
@Configuration
public class ZooKeeperCuratorConfig {
    @Value("${zookeeper.url}")
    private String zkUrl;

    @Bean
    public CuratorFramework curatorFramework(){
        // 5次 一个 1000 ms
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,5);
        CuratorFramework curatorFramework = CuratorFrameworkFactory.builder()
                .connectString(zkUrl)
                .retryPolicy(retryPolicy)
                .build();
        curatorFramework.start();;
        return curatorFramework;
    }
}
